﻿using System;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace MJTop.Common
{
    public class CaptchaControlDesigner : ControlDesigner
    {
        private Captcha authCodeImageHandler;

        private CaptchaControl componentRef;

        private string designTimeAuthCodeImageSavingPath;

        public CaptchaControlDesigner()
        {
        }

        public override string GetDesignTimeHtml()
        {
            string emptyDesignTimeHtml;
            this.authCodeImageHandler.GenerateDesignTimeCaptchaImage(this.designTimeAuthCodeImageSavingPath);
            if (this.componentRef.LivePreview != LivePreviewMode.On)
            {
                emptyDesignTimeHtml = this.GetEmptyDesignTimeHtml();
            }
            else
            {
                string[] str = new string[] { "<img src=\"", this.designTimeAuthCodeImageSavingPath, "\" style=\"width:", null, null, null, null, null, null, null, null };
                Unit width = this.componentRef.Width;
                str[3] = width.ToString();
                str[4] = "; height:";
                width = this.componentRef.Height;
                str[5] = width.ToString();
                str[6] = "; border-width:";
                width = this.componentRef.BorderWidth;
                str[7] = width.ToString();
                str[8] = "; border-style:";
                str[9] = this.componentRef.BorderStyle.ToString();
                str[10] = "\" />";
                emptyDesignTimeHtml = string.Concat(str);
            }
            return emptyDesignTimeHtml;
        }

        protected override string GetEmptyDesignTimeHtml()
        {
            string[] str = new string[] { "<input type=\"text\" value=\"验证码图片\" style=\"width:", null, null, null, null };
            Unit width = this.componentRef.Width;
            str[1] = width.ToString();
            str[2] = "; height:";
            width = this.componentRef.Height;
            str[3] = width.ToString();
            str[4] = "; background-color:#EFEFEF;border-width:1px;border-style:Dashed\" />";
            return string.Concat(str);
        }

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            this.componentRef = (CaptchaControl)component;
            this.authCodeImageHandler = new Captcha();
            this.designTimeAuthCodeImageSavingPath = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "\\DesignTimeAuthCodeImage.png");
            this.componentRef.Width = this.componentRef.Width;
            this.componentRef.Height = this.componentRef.Height;
        }
    }
}